﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RegionActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The region actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Locations
{
    using System;

    using NHibernate;

    using ServiceLibrary.Models.Base;
    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Commands;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The region actioner.
    /// </summary>
    public class RegionActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RegionActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public RegionActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create region.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="country">
        /// The country.
        /// </param>
        /// <param name="info">
        /// The info.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateRegion(string name, Country country, string info)
        {
            var where = new Region();
            where.ID = Guid.NewGuid();
            where.Name = name;
            where.CreatedDateTime = DateTime.Now;
            where.Country = country;
            where.AdditionalInformation = info;
            this.Session.Save(where);

            this.Log.CreateLogEntry(
                where.ID, 
                string.Format("Region '{0}, {1}', blev oprettet.", where.Name, where.Country.Name), 
                Enums.LogEntryType.Region, 
                Enums.LogEntryActionType.Create);

            return where.ID;
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="RegionsResult"/>.
        /// </returns>
        public RegionsResult ExecuteSearchQuery(RegionsQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new RegionsResult();

            IQueryOver<Region, Region> search = this.Session.QueryOver<Region>();
            this.ApplyDefaultCriterias(settings, search);
            
            int rowCount = search.RowCount();

            var regions = this.ApplySkipAndTake(settings, search.OrderBy(x => x.Name).Asc);
            
            result.TotalRowCount = rowCount;

            foreach (Region region in regions)
            {
                Services.Dto.Region dto = Services.Dto.Region.Convert(region);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get region.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="Region"/>.
        /// </returns>
        public Region GetRegion(Guid id)
        {
            var where = this.Session.Get<Region>(id);

            return where;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The create region.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void CreateRegion(CreateRegionCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var reg = new Region
                              {
                                  ID = cmd.Dto.ID, 
                                  Name = cmd.Dto.Name, 
                                  AdditionalInformation = cmd.Dto.AdditionalInformation, 
                                  Country = this.Session.Get<Country>(cmd.Dto.Country)
                              };

                this.Session.Save(reg);

                this.Log.CreateLogEntry(
                    reg.ID, 
                    string.Format("Region '{0}, {1}', blev oprettet.", reg.Name, reg.Country.Name),
                    Enums.LogEntryType.Region,
                    Enums.LogEntryActionType.Create);
            }
        }

        /// <summary>
        /// The delete region.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void DeleteRegion(DeleteRegionCommand cmd)
        {
            var reg = this.Session.Get<Region>(cmd.Dto);

            if (reg != null)
            {
                var regionUsage = this.Session.QueryOver<Location>().Where(x => x.Region.ID == reg.ID).RowCount();
                if (regionUsage == 0)
                {
                    this.Session.Delete(reg);
                }
            }
        }

        /// <summary>
        /// The update region.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void UpdateRegion(UpdateRegionCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var reg = this.Session.Get<Region>(cmd.Dto.ID);
                if (reg != null)
                {
                    reg.Name = cmd.Dto.Name;
                    reg.AdditionalInformation = cmd.Dto.AdditionalInformation;
                    reg.Country = this.Session.Get<Country>(cmd.Dto.Country);
                    this.Session.Update(reg);
                }
            }
        }

        #endregion
    }
}